package com.j180.config;

import com.j180.converter.StringToLocalDate;

import com.j180.interceptor.*;

import com.j180.interceptor.StoreInterceptor;
import com.j180.interceptor.SupplierInterceptor;
import com.j180.interceptor.UserInterceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import org.springframework.web.servlet.config.annotation.*;

@Configuration
@ComponentScan("com.j180.*")
@Import(MybatisConfig.class)
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer{
    //处理静态文件的
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToLocalDate());
    }

    //要使用springMvc的文件上传功能时，需注册一个（二进制编码的解析器）
    //注意： 在注册MultipartResolver时 方法的名字必须时multipartResolver
    @Bean
    public MultipartResolver multipartResolver(){

        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        //设置文件的大小，不设置的话默认时没有限制
        //commonsMultipartResolver.setMaxUploadSize(1024*200);
        return commonsMultipartResolver;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new StoreInterceptor()).addPathPatterns("/store");
        registry.addInterceptor(new UserInterceptor()).addPathPatterns("/user");
        registry.addInterceptor(new SupplierInterceptor()).addPathPatterns("/sup");

        registry.addInterceptor(new SupGoodsInterceptor()).addPathPatterns("/supGoods");
        registry.addInterceptor(new AllIntercepter()).addPathPatterns("/**").excludePathPatterns();

    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        //configurer.enable();
    }
}